﻿//====================================================================
// Create by Daniel
// file： YCUserAction.cs
// project ：YunCMS
// Build time：2012-09-22
// modify:
// description:
// ===================================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using YunCMS.Model;
using YunCMS.IDAL;
//using Utility;

namespace YunCMS.DAL
{
    /// <summary>
    /// Data Access Layer of dbo.YC_User_Action.
    /// </summary>
    public partial class YCUserActionDAL : IYCUserActionDAL
    {
		#region 
		
		/// <summary>
		/// constractor
		/// </summary>
		public YCUserActionDAL()
		{
		}
		#endregion

        #region -----------Methods-----------
		/// <summary>
		/// Insert a model to Database
		/// </summary>
		/// <param name="model">YCUserActionnew model</param>
		/// <returns>The new item Id</returns>
		public int Add(YCUserAction model)
		{
			StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("insert into YC_User_Action(");
            sqlStr.Append("ID,Name,Note,ActionType,Module,Category,ActionValue,PageUrl)");
            sqlStr.Append(" values (");
            sqlStr.Append("@ID,@Name,@Note,@ActionType,@Module,@Category,@ActionValue,@PageUrl)");
			int res;

			res = SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, false));
			return res;
		}
		/// <summary>
		/// Insert a model to Database in transaction.
		/// </summary>
        /// <param name="trans">SqlTransaction</param>
		/// <param name="model">YCUserActionnew model</param>
		/// <returns>The new item Id</returns>
		public int Add(SqlTransaction trans,YCUserAction model)
		{
			StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("insert into YC_User_Action(");
            sqlStr.Append("ID,Name,Note,ActionType,Module,Category,ActionValue,PageUrl)");
            sqlStr.Append(" values (");
            sqlStr.Append("@ID,@Name,@Note,@ActionType,@Module,@Category,@ActionValue,@PageUrl)");
			int res;			
			
			res = SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, false));
			return res;
		}
		/// <summary>
		/// update a model to db
		/// </summary>
		/// <param name="model">YCUserAction</param>
		/// <returns>rows affected</returns>
		public int Update(YCUserAction model)
		{
            StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("update YC_User_Action set ");
            sqlStr.Append("ID=@ID,Name=@Name,Note=@Note,ActionType=@ActionType,Module=@Module,Category=@Category,ActionValue=@ActionValue,PageUrl=@PageUrl");
			sqlStr.Append(" where ID=@ID");
			int res;
            
			res = SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, true));
			return res;
		}
		
        /// <summary>
		/// update a model to db in transaction
		/// </summary>
        /// <param name="trans">SqlTransaction</param>
		/// <param name="model">YCUserAction</param>
		/// <returns>rows affected</returns>
		public int Update(SqlTransaction trans,YCUserAction model)
		{
            StringBuilder sqlStr = new StringBuilder();
			sqlStr.Append("update YC_User_Action set ");
            sqlStr.Append("ID=@ID,Name=@Name,Note=@Note,ActionType=@ActionType,Module=@Module,Category=@Category,ActionValue=@ActionValue,PageUrl=@PageUrl");
			sqlStr.Append(" where ID=@ID");
			int res;
	
			res = SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),PrepareSqlParams(model, true));
			return res;
		}
		
		/// <summary>
		/// delete a model from db
		/// </summary>
	    /// <param name="id">ID</param>
		/// <returns>rows affected</returns>
		public int Delete(int id)
		{
			StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("delete YC_User_Action ");
            sqlStr.Append(" where ID=@ID");
			SqlParameter[] parameters={
							new SqlParameter("@ID",SqlDbType.Int),
			
			};
			parameters[0].Value=id;
			return SqlHelper.ExecuteNonQuery(CommandType.Text,sqlStr.ToString(),parameters);
		}
		
        /// <summary>
		/// delete a model from db in transaction
		/// </summary>        
        /// <param name="trans">SqlTransaction</param>
	    /// <param name="id">ID</param>
		/// <returns>rows affected</returns>
		public int Delete(SqlTransaction trans,int id)
		{
			StringBuilder sqlStr = new StringBuilder();
            sqlStr.Append("delete YC_User_Action ");
            sqlStr.Append(" where ID=@ID");
			SqlParameter[] parameters={
							new SqlParameter("@ID",SqlDbType.Int),
			
			};
			parameters[0].Value=id;
			return SqlHelper.ExecuteNonQuery(trans,CommandType.Text,sqlStr.ToString(),parameters);
		}
		
        /// <summary>
		///Get YCUserAction Entity
		/// </summary>
		/// <param name="dr">dr</param>
		/// <returns>YCUserAction Entity</returns>
		public YCUserAction PopulateYCUserActionFromDr(IDataReader dr)
		{
			YCUserAction Obj = new YCUserAction();
			
				Obj.ID = Convert.ToInt32( dr["ID"]);
				Obj.Name =  dr["Name"].ToString();
                Obj.Note =  dr["Note"].ToString();
                Obj.ActionType =  dr["ActionType"].ToString();
                Obj.Module =  dr["Module"].ToString();
                if(( dr["Category"])==DBNull.Value){ Obj.Category = null; } else { Obj.Category = Convert.ToInt32( dr["Category"]);}
                Obj.ActionValue =  dr["ActionValue"].ToString();
                Obj.PageUrl =  dr["PageUrl"].ToString();
			
			return Obj;
		}
		
		/// <summary>
		/// get a YCUserAction object 
		/// </summary>
		/// <param name=id>ID</param>
		/// <returns>YCUserAction object</returns>
		public YCUserAction GetYCUserAction (int id)
		{
			YCUserAction _obj=null;
			SqlParameter[] parameters={
			new SqlParameter("@ID",SqlDbType.Int)
			};
			parameters[0].Value=id;
			string sqlStr="select * from YC_User_Action where ID=@ID";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr,parameters))
			{
				while(dr.Read())
				{
            		_obj = PopulateYCUserActionFromDr(dr);
				}
			}
			return _obj;
		}
		/// <summary>
		/// Get YCUserActions List
		/// </summary>
		/// <returns>List</returns>
		public IList<YCUserAction> GetList(string sqlStr)
		{
			IList< YCUserAction> Obj=new List< YCUserAction>();
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr))
			{
				while(dr.Read())
				{
					Obj.Add(PopulateYCUserActionFromDr(dr));
				}
			}
			return Obj;
		}

		/// <summary>
        /// if exist a YCUserAction according by ID
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>true/false</returns>
		public bool IsExist(int id)
		{
			SqlParameter[] parameters={
                                      new SqlParameter("@ID",SqlDbType.Int)
                                  };
            parameters[0].Value=id;
            string sqlStr="select Count(1) from YC_User_Action where ID=@ID";
            int a=Convert.ToInt32(SqlHelper.ExecuteScalar(CommandType.Text,sqlStr,parameters));
            if(a>0)
            {
                return true;
            }
            else
            {
                return false;
            }
		}
        
        private SqlParameter[] PrepareSqlParams(YCUserAction model, bool isUpdate)
        {
     			 List<SqlParameter> parameters = new List<SqlParameter>() {							
							new SqlParameter("@ID",SqlDbType.Int),
							new SqlParameter("@Name",SqlDbType.NVarChar),
							new SqlParameter("@Note",SqlDbType.NVarChar),
							new SqlParameter("@ActionType",SqlDbType.VarChar),
							new SqlParameter("@Module",SqlDbType.VarChar),
							new SqlParameter("@Category",SqlDbType.Int),
							new SqlParameter("@ActionValue",SqlDbType.VarChar),
							new SqlParameter("@PageUrl",SqlDbType.NVarChar)
			};
                parameters[0].Value = model.ID;                
                parameters[1].Value = model.Name;                
			if(model.Note!= String.Empty)
				parameters[2].Value = model.Note;
			else
				parameters[2].Value = DBNull.Value;				
			if(model.ActionType!= String.Empty)
				parameters[3].Value = model.ActionType;
			else
				parameters[3].Value = DBNull.Value;				
			if(model.Module!= String.Empty)
				parameters[4].Value = model.Module;
			else
				parameters[4].Value = DBNull.Value;				
			if(model.Category!= null)
				parameters[5].Value = model.Category;
			else
				parameters[5].Value = DBNull.Value;				
			if(model.ActionValue!= String.Empty)
				parameters[6].Value = model.ActionValue;
			else
				parameters[6].Value = DBNull.Value;				
			if(model.PageUrl!= String.Empty)
				parameters[7].Value = model.PageUrl;
			else
				parameters[7].Value = DBNull.Value;				
            
            if (isUpdate)
            {
                SqlParameter param = new SqlParameter("@ID",SqlDbType.Int);
                param.Value = model.ID; 
                parameters.Add(param);
            }

            return parameters.ToArray();
        }
        
		/*
		public int GetMaxId()
		{
			int maxid;
			string sqlStr="select max(ID) from YC_User_Action order by ID desc";
			using(SqlDataReader dr=SqlHelper.ExecuteReader(CommandType.Text,sqlStr))
			{
				if(dr.Read())
				{
					maxid = Convert.ToInt32( dr["ID"]);
				}
				else
					maxid = 0;
			}
			return maxid;
		}

		*/
        #endregion
		
    }
}




